package pt.agap2.relationchat;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import pt.agap2.relationchat.utils.Message;
import pt.agap2.relationchat.utils.MockServerCommunication;
import pt.agap2.relationchat.utils.UserData;
import pt.agap2.relationchat.utils.Constants;

public class UserMessageActivity extends Activity implements View.OnClickListener{

    private ListView listView;
    private EditText txtMessage;
    private Button btnSendMessage;
    private String appToken;
    private String userToken;
    private String personId;
    private MockServerCommunication msc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_message);

        listView = (ListView) findViewById(R.id.userMessagelistView);
        txtMessage = (EditText) findViewById(R.id.userMessageTxtMessage);
        btnSendMessage = (Button) findViewById(R.id.userMessageBtnSend);
        btnSendMessage.setOnClickListener(this);

        personId = this.getIntent().getExtras().get(Constants.KEY_PERSON_ID).toString();
    }

    @Override
    protected void onResume(){
        super.onResume();
        SharedPreferences settings = getSharedPreferences(Constants.APP_TOKEN, 0);
        appToken = settings.getString(Constants.KEY_APP_TOKEN, Constants.APP_TOKEN);
        userToken = settings.getString(Constants.KEY_USER_TOKEN, Constants.EMPTY_STRING);

        msc = new MockServerCommunication();
        Message[] aMessages = msc.getMessages(appToken, userToken, personId);

        for (int i = 0; i <aMessages.length; i++){
            Message m = aMessages[i];
        }
    }

    private void sendMessage(){
        MockServerCommunication msc = new MockServerCommunication();
        msc.sendMessage(appToken, userToken, personId, txtMessage.getText().toString());
        txtMessage.setText(Constants.EMPTY_STRING);
    }

    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.userMessageBtnSend:
                sendMessage();
                break;
        }
    }
}
